#define _CRT_SECURE_NO_WARNINGS 1
#include<stdio.h>
int* binary(int a) {
    int arr[4] = {0};
    int n = 0;
    while (a) {
        arr[n++] = a % 2;
        a /= 2;
    }
    return arr;
}
int main()
{
    int i = 0;
    scanf("%d", &i);
    for (int j = 0; j < i; j++) {
        int a = 0;
        int b = 0;
        int w0, w1, w2, w3;
        scanf("%d", &a);
        scanf("%d", &b);

        scanf("%d", &w0);
        scanf("%d", &w1);
        scanf("%d", &w2);
        scanf("%d", &w3);
        int arr[4] = { 0 };
        int n = 0;
        while (a) {
            arr[n++] = a % 2;
            a /= 2;
        }
        int brr[4] = { 0 };
        n = 0;
        while (b) {
            brr[n++] = a % 2;
            b /= 2;
        }
        int crr[4] = { 0 };
        for (int n = 0; n < 4; n++) {
            if (arr[n] == 0 && brr[n] == 0) {
                crr[n] = w0;
            }
            else if (arr[n] == 0 && brr[n] == 1) {
                crr[n] = w1;
            }
            else if (arr[n] == 1 && brr[n] == 0) {
                crr[n] = w2;
            }
            else if (arr[n] == 1 && brr[n] == 1) {
                crr[n] = w3;
            }
        }
        printf("%d\n", crr[0] * 8 + crr[1] * 4 + crr[2] * 2 + crr[3] * 1);
    }

    return 0;
}
int main5()
{
    double a = 0;
    char b = '0';
    double c = 0;
    scanf("%lf%c%lf", &a, &b, &c);
    double ret = a / c;
    printf("%.2f", ret);
    return 0;
  
}
int main4()
{
    int arr[10] = { 0 };
    int brr[2] = { 0 };
    int n = 0;
        for (int i = 1000; i <= 9999; i++) {
            int b = i;
            for (int j = 0; j < 2; j++) {
                brr[j] = b % 100;
                b /= 100;
            }
            if (brr[0] * brr[0] + brr[1] * brr[1] == i) {
                arr[n] = i;
                n++;
            }
        }
        for (int i = 0; i < n; i++) {
            printf("%d", arr[i]);
        }
    return 0;
}
int main2() {
    int a = 0;
    scanf("%d", &a);
    int arr[10] = { 0 };
    int i = 0;
    while (a) {
        arr[i] = a % 10;
        a /= 10;
        i++;
    }
    i--;
    for (; i >= 4; i--) {
        printf("%d", arr[i]);
    }
    for (int i = 0; i < 4; i++) {
        printf("%d", arr[i]);
    }
    return 0;
    
}
int main1() {
    int n = 0;
    scanf("%d", &n);
    int i = 1;
    int L = 0;
    while (n) {
        L += n % 10 * i;
        n /= 10;
        i++;

    }
    printf("%d", L);
    return 0;
}



        